package gestion;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.datatype.XMLGregorianCalendar;

import org.omg.CORBA.MARSHAL;
import org.xml.sax.InputSource;

import business.CarnetType;
import business.RendezVoussType;

public class CarnetReader 
{
	public static void main(String[] args) {
		CarnetReader cr    = new CarnetReader("FMcarnet.xml");
		System.out.println(cr.toString());
		System.out.println("---------------------------");
		Calendar instance  = Calendar.getInstance();
		instance.set(2010,0, 27);
		RendezVoussType rd = cr.getRendezVous(instance.getTime());
		StringBuffer message = new StringBuffer();
		cr.afficher(message, rd);
		System.out.println("Rendez-Vous avec : \n" +message.toString());
		
	}
		
	protected String fileName;

	public CarnetReader(String fileName) {
		this.fileName = fileName;
	}

	/** A partir d'une date retourne le prochaine rendez-vous*/
	public RendezVoussType getRendezVous(Date date)
	{
		try {
			JAXBContext context = JAXBContext.newInstance("business");
			// aller de xml vers java decodeur
			Unmarshaller decoder  = context.createUnmarshaller();
			try {
				InputSource  inputXml      = new InputSource(new FileReader(fileName));
				JAXBElement<?> element     = (JAXBElement<?>)decoder.unmarshal(inputXml);
				CarnetType ct 			   = (CarnetType)element.getValue();
				List<RendezVoussType> lesRendez = ct.getRendezVous();
				RendezVoussType resultat   = null;
				for(RendezVoussType rdt : lesRendez){
					XMLGregorianCalendar calender = rdt.getDebut();
					Calendar unecalender = calender.toGregorianCalendar();
					Date uneDate         = unecalender.getTime();
					
					if(uneDate.after(date)){
						
						if(resultat == null)
							resultat = rdt;
						else
						{
							 Date d = resultat.getDebut().toGregorianCalendar().getTime();
							 if(uneDate.after(d))
								 resultat = rdt;
						}
					}
				}
				return resultat;
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}			
		} catch (JAXBException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	// affiche tous les rendez vous du carnet XML 
	@Override
	public String toString() {
		StringBuffer message =  new StringBuffer();
		/// on utilise JaxbContext pour convertr de xml ver java et vice-versa
		try {
			JAXBContext context = JAXBContext.newInstance("business");
			// aller de xml ver java
			Unmarshaller decoder  = context.createUnmarshaller();
			try {
				InputSource  inputXml      = new InputSource(new FileReader(fileName));
				JAXBElement<?> element     = (JAXBElement<?>)decoder.unmarshal(inputXml);
				CarnetType ct 			   = (CarnetType)element.getValue();
				List<RendezVoussType> lesRendez = ct.getRendezVous();
				for(RendezVoussType rdt : lesRendez){
					afficher(message, rdt);
					System.out.println(message.toString());
				}
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		} catch (JAXBException e) {
			e.printStackTrace();
		}
		return message.toString();
	}

	private void afficher(StringBuffer message, RendezVoussType rdt) {
		XMLGregorianCalendar calender = rdt.getDebut();
		Calendar unecalender = calender.toGregorianCalendar();
		Date uneDate         = unecalender.getTime();
		SimpleDateFormat dsf = new SimpleDateFormat("dd MM yyyy");
		message.append("\nDate    : " + dsf.format(uneDate));
		message.append("\nDuree   : " + rdt.getDuree());
		message.append("\nLieu    : " + rdt.getLieu());
		message.append("\nSujet   : " + rdt.getSujet());
		message.append("\nContact : " + rdt.getContact());
	}
	   

}
